<?php
require_once '../../a_config.php';
require_once "{$A_CONFIG['api_include']}";
require_once '../../api_header.php';
$conn = connect_to_db();
$err = -1;
$msg = "";
$page_num = 10;//每页显示数量
$condition = array();
$aa="";
if (isset($_POST['token']) && !empty($_POST["token"]) &&
    isset($_POST['time']) && !empty($_POST["time"]) &&
    isset($_POST["username"]) && !empty($_POST["username"]) &&
    isset($_POST["passwd"]) && !empty($_POST["passwd"])&&
    isset($_POST["id"]) && !empty($_POST["id"])
) {
    $key = $A_CONFIG['api_key'];
    $unixtime = $_POST['time'];
    $token = md5($key . $unixtime);
    if ($token == $_POST["token"]) {
        $username = mysql_real_escape_string($_POST["username"]);
        $passwd = mysql_real_escape_string($_POST["passwd"]);
        $id = mysql_real_escape_string($_POST["id"]);
        if( isset($_POST["set_people_auth"]) && !empty($_POST["set_people_auth"]) && $_POST["set_people_auth"]=='true'){
            $set_people_auth =1;
        }else{
            $set_people_auth=0;
        }
        $aa.=" , people_auth=$set_people_auth ";
        if( isset($_POST["set_room_auth"]) && !empty($_POST["set_room_auth"]) && $_POST["set_room_auth"]=='true'){
            $set_room_auth =1;
        }else{
            $set_room_auth=0;
        }
        $aa.=" , room_auth=$set_room_auth ";
        if( isset($_POST["set_join_auth"]) && !empty($_POST["set_join_auth"]) && $_POST["set_join_auth"]=='true'){
            $set_join_auth =1;
        }else{
            $set_join_auth=0;
        }
        $aa.=" , join_auth=$set_join_auth ";
        if( isset($_POST["set_store_auth"]) && !empty($_POST["set_store_auth"]) && $_POST["set_store_auth"]=='true'){
            $set_store_auth =1;
        }else{
            $set_store_auth=0;
        }
        $aa.=" , store_auth=$set_store_auth ";
        if( isset($_POST["set_admin_auth"]) && !empty($_POST["set_admin_auth"]) && $_POST["set_admin_auth"]=='true'){
            $set_admin_auth =1;
        }else{
            $set_admin_auth=0;
        }
        $aa.=" , admin_auth=$set_admin_auth ";
        $where = "where is_del=0 and username='$username' and id!='$id' limit 1";
        $s_db_name = $A_CONFIG['project_pre'] . '_backstage_admin_list';
        $sql = "SELECT id FROM {$s_db_name} $where";
        $res = mysql_query($sql, $conn);
        if (is_resource($res) && mysql_num_rows($res) == 0) {
            $sql = "update {$s_db_name} set `username`='$username',`password`='$passwd',update_time=now() $aa where id='$id' ";
            $res2 = mysql_query($sql, $conn);
            if ($res2) {
                $msg = "修改成功";
                $err = 0;
            } else {
                $err = -2;
                $msg = "未进行任何修改";
            }
        } else {
            $err = -3;
            $msg = "该用户名已经存在";
        }
    } else {
        $err = -4;
        $msg = "数据处理出错";

    }


} else {
    $err = -5;
    $msg = "参数错误";
}
if (!$A_CONFIG['is_debug']) {
    $resArr = compact("err", "msg");
} else {
    $resArr = compact("err", "msg", 'sql',"aa");
}
ob_clean();
$resJson = json_encode_cn($resArr);
echo $resJson;
